/*
 * $Id: D6Roller.java 7 2009-12-09 02:52:31Z dave.terhune $
 *
 * Copyright (C) 2009 David Terhune
 *
 * This file is part of DiceRoller.
 *
 * DiceRoller is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * DiceRoller is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with DiceRoller.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.dterhune.gaming.dice;

import android.app.Activity;
import android.view.View;
import android.widget.TextView;
import java.util.Random;

/** Rolls dice for Generic d6 games. */
@SuppressWarnings("serial") // Never actually serialized, so ignore warning
class D6Roller extends Random implements View.OnClickListener
{
    /** Number of items to print per line. */
    private static final int ITEMS_PER_LINE = 10;

    /** Result text field. */
    TextView result;
    /** Sum text field. */
    TextView sum;
    
    /** 
     * Creates a new D6Roller.
     * @param activity The parent DiceRoller
     */
    public D6Roller(Activity activity)
    {
        // Set the click listener
        View b = activity.findViewById(R.id.d6p1);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.d6p2);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.d6p3);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.d6p4);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.d6p5);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.d6p6);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.d6p7);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.d6p8);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.d6p9);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.d6p10);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.d6p11);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.d6p12);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.d6p13);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.d6p14);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.d6p15);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.d6p16);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.d6p17);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.d6p18);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.d6p19);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.d6p20);
        b.setOnClickListener(this);

        // Cache the text fields
        result = (TextView)activity.findViewById(R.id.d6_result);
        sum = (TextView)activity.findViewById(R.id.d6_sum);
    }

    /**
     * Click handler.
     * @param v view that was clicked
     */
    public void onClick(View v)
    {
        // Figure out how many dice to roll
        int dice = 0;
        switch(v.getId())
        {
            case R.id.d6p1:  dice =  1; break;
            case R.id.d6p2:  dice =  2; break;
            case R.id.d6p3:  dice =  3; break;
            case R.id.d6p4:  dice =  4; break;
            case R.id.d6p5:  dice =  5; break;
            case R.id.d6p6:  dice =  6; break;
            case R.id.d6p7:  dice =  7; break;
            case R.id.d6p8:  dice =  8; break;
            case R.id.d6p9:  dice =  9; break;
            case R.id.d6p10: dice = 10; break;
            case R.id.d6p11: dice = 11; break;
            case R.id.d6p12: dice = 12; break;
            case R.id.d6p13: dice = 13; break;
            case R.id.d6p14: dice = 14; break;
            case R.id.d6p15: dice = 15; break;
            case R.id.d6p16: dice = 16; break;
            case R.id.d6p17: dice = 17; break;
            case R.id.d6p18: dice = 18; break;
            case R.id.d6p19: dice = 19; break;
            case R.id.d6p20: dice = 20; break;
        }

        int total = 0;
        StringBuilder sb = new StringBuilder();

        // Roll the dice
        for(int i = 0; i < dice; i++)
        {
            // Append new line or comma
            if(i != 0 && (i % ITEMS_PER_LINE) == 0)
            {
                sb.append('\n');
            }
            else if(i != 0)
            {
                sb.append(", ");
            }
            int d = nextInt(6) + 1;
            total += d;
            sb.append(d);
        }

        // Display the results
        result.setText(sb);
        sum.setText(Integer.toString(total));
    }
}
